package com.aidex.web.controller.biz;

import com.aidex.biz.domain.ElectronFence;
import com.aidex.biz.service.ElectronFenceService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 回收员电子围栏Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-09-28
 */
@RestController
@RequestMapping("/biz/bizElectronFence")
public class ElectronFenceController extends BaseController {
    @Autowired
    private ElectronFenceService electronFenceService;

    /**
     * 查询回收员电子围栏列表
     */
    @GetMapping("/list")
    public R<PageInfo> list(ElectronFence electronFence, HttpServletRequest request, HttpServletResponse response) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            electronFence.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        electronFence.setPage(new PageDomain(request, response));
        return R.data(electronFenceService.findPage(electronFence));
    }

    /**
     * 获取回收员电子围栏详细信息
     */
    @GetMapping(value = "/{id}")
    public R<ElectronFence> detail(@PathVariable("id") String id) {
        return R.data(electronFenceService.get(id));
    }

    /**
     * 新增回收员电子围栏
     */
    @Log(title = "回收员电子围栏", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated ElectronFence electronFence) {
        return R.status(electronFenceService.save(electronFence));
    }

    /**
     * 修改回收员电子围栏
     */
    @Log(title = "回收员电子围栏", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated ElectronFence electronFence) {
        return R.status(electronFenceService.save(electronFence));
    }


    /**
     * 删除回收员电子围栏
     */
    @Log(title = "回收员电子围栏", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(electronFenceService.deleteElectronFenceByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(electronFenceService.findMaxSort(new ElectronFence()));
    }

    /**
     * 导出回收员电子围栏列表
     */
    @Log(title = "回收员电子围栏", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(ElectronFence electronFence) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            electronFence.setCompanyId(null);
        } else {
            electronFence.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<ElectronFence> list = electronFenceService.findList(electronFence);
        ExcelUtil<ElectronFence> util = new ExcelUtil<ElectronFence>(ElectronFence.class);
        return util.exportExcel(list, "回收员电子围栏数据");
    }

}
